﻿Imports DTO
Imports System.Data.SqlClient
Public Class SANHDAO
    Private ketnoi As New DAO.connect()
    Public Function loadsanh() As DataTable
        ketnoi.OpenConnection()
        Dim dtnv As New DataTable()
        Dim chuoi As String = "select * from SANH"
        dtnv = ketnoi.getDataTable(chuoi)
        ketnoi.CloseConnection()
        Return dtnv
    End Function
    Private Function Sqlcommand_THEM(ByVal MASANH As Integer, ByVal TENSANH As String, ByVal SOLUONGBANTOIDA As Integer, ByVal GHICHU As String, ByVal MALOAISANH As Integer) As Integer
        ketnoi.OpenConnection()
        Dim i As Integer = 0

        Dim sqlcmd As New SqlCommand("Stor_THEM_SANH", ketnoi.getConnection)
        sqlcmd.CommandType = CommandType.StoredProcedure
        'sqlcmd.Parameters.Add("@mode", SqlDbType.Int).Value = mode;
        sqlcmd.Parameters.Add("@MALOAISANH", SqlDbType.Int).Value = MALOAISANH
        sqlcmd.Parameters.Add("@MASANH", SqlDbType.Int).Value = MALOAISANH
        sqlcmd.Parameters.Add("@TENSANH", SqlDbType.NVarChar).Value = TENSANH
        sqlcmd.Parameters.Add("@SOLUONGBANTOIDA", SqlDbType.Float).Value = SOLUONGBANTOIDA

        sqlcmd.Parameters.Add("@GHICHU", SqlDbType.NVarChar).Value = GHICHU

        i = sqlcmd.ExecuteNonQuery()
        ketnoi.CloseConnection()
        Return i
    End Function
    Public Function them_SANH(ByVal dto As SANHDTO)
        ketnoi.OpenConnection()
        'Dim dto As New LOAISANHDTO()
        If Sqlcommand_THEM(dto.MASANH, dto.TENSANH, dto.SOLUONGBANTOIDA, dto.GHICHU, dto.MALOAISANH) <> 0 Then
            ketnoi.CloseConnection()
            Return True
        End If
        ketnoi.CloseConnection()
        Return False
    End Function

    Private Function Sqlcommand_capnhat(ByVal MASANH As Integer, ByVal TENSANH As String, ByVal SOLUONGBANTOIDA As Integer, ByVal GHICHU As String, ByVal MALOAISANH As Integer) As Integer
        ketnoi.OpenConnection()
        Dim i As Integer = 0

        Dim sqlcmd As New SqlCommand("STOR_CAPNHAT_SANH", ketnoi.getConnection)
        sqlcmd.CommandType = CommandType.StoredProcedure

        sqlcmd.Parameters.Add("@MALOAISANH", SqlDbType.Int).Value = MALOAISANH
        sqlcmd.Parameters.Add("@MASANH", SqlDbType.Int).Value = MALOAISANH
        sqlcmd.Parameters.Add("@TENSANH", SqlDbType.NVarChar).Value = TENSANH
        sqlcmd.Parameters.Add("@SOLUONGBANTOIDA", SqlDbType.Float).Value = SOLUONGBANTOIDA

        sqlcmd.Parameters.Add("@GHICHU", SqlDbType.NVarChar).Value = GHICHU


        i = sqlcmd.ExecuteNonQuery()
        ketnoi.CloseConnection()
        Return i
    End Function
    Public Function capnhat_sanh(ByVal dto As SANHDTO) As Boolean
        ketnoi.OpenConnection()
        If Sqlcommand_capnhat(dto.MASANH, dto.TENSANH, dto.SOLUONGBANTOIDA, dto.GHICHU, dto.MALOAISANH) <> 0 Then
            ketnoi.CloseConnection()
            Return True
        End If
        ketnoi.CloseConnection()
        Return False

    End Function

    Private Function Sqlcommand_XOA(ByVal MASANH As Integer) As Integer
        ketnoi.OpenConnection()
        Dim i As Integer = 0

        Dim sqlcmd As New SqlCommand("Stor_XOA_SANH", ketnoi.getConnection)
        sqlcmd.CommandType = CommandType.StoredProcedure
        sqlcmd.Parameters.Add("@MASANH", SqlDbType.Int).Value = MASANH

        i = sqlcmd.ExecuteNonQuery()
        ketnoi.CloseConnection()
        Return i
    End Function
    Public Function xoa_SANH(ByVal dto As SANHDTO) As Boolean
        ketnoi.OpenConnection()
        If Sqlcommand_XOA(dto.MASANH) <> 0 Then
            ketnoi.CloseConnection()
            Return True
        End If
        ketnoi.CloseConnection()
        Return False

    End Function

End Class
